package org.springblade.system.excel;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ExcelSelectorSheetWriteHandler implements SheetWriteHandler {
	private Class clazz;

	public ExcelSelectorSheetWriteHandler(Class clazz) {
		this.clazz = clazz;
	}
	public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
	}


	@Override
	public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
		Workbook workbook = writeWorkbookHolder.getWorkbook();

		Sheet sheet = writeSheetHolder.getSheet();
		DataValidationHelper helper = sheet.getDataValidationHelper();
		if (clazz==null) {
			return;
		}
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			if (!field.isAnnotationPresent(ExcelSelector.class)) {
				continue;
			}
			ExcelSelector excelSelector = field.getAnnotation(ExcelSelector.class);
			int column = excelSelector.columnIndex();
			int row = excelSelector.rowIndex();
			String[] strings = excelSelector.fixedSelector();
			String key = excelSelector.dictKeyValue();
			ExcelSelector.ValueType valueType = excelSelector.valueType();
			if (StringUtils.hasLength(key)) {
				Map<String, Object> stringObjectMap;
				if (valueType == ExcelSelector.ValueType.MAIN) {
					stringObjectMap = ExcelSelectorCache.get(key);
				} else {
					key = key+ExcelSelectorCache.sub;
					stringObjectMap = ExcelSelectorCache.get(key);
				}
				if (stringObjectMap == null) {
					stringObjectMap = new HashMap<>();
				}

				String[] selectorData = (String[]) stringObjectMap.get(ExcelSelectorCache.selector);
				if (selectorData == null) {
					selectorData = new String[0];
				}
				strings = selectorData;

			}


			if (strings.length < 10) {
				if (strings.length < 1) {
					continue;
				}
				// 下拉 首行 末行 首列 末列
				CellRangeAddressList list = new CellRangeAddressList(row, row, column, column);
				// 下拉值
				DataValidationConstraint constraint = helper.createExplicitListConstraint(strings);
				DataValidation validation = helper.createValidation(constraint, list);
				sheet.addValidationData(validation);
			} else {
				Sheet sheet1 = workbook.getSheet(key);
				if (sheet1 == null) {
					Sheet proviceSheet = workbook.createSheet(key);
					for (int i = 0, length = strings.length; i < length; i++) {
						// i:表示你开始的行数  0表示你开始的列数
						proviceSheet.createRow(i).createCell(0).setCellValue(strings[i]);
					}
					Name category1Name = workbook.createName();
					category1Name.setNameName(key);
					//4 $A$1:$A$N代表 以A列1行开始获取N行下拉数据
					category1Name.setRefersToFormula(key + "!$A$1:$A$" + (strings.length));
				}

				//5 将刚才设置的sheet引用到你的下拉列表中
				CellRangeAddressList list = new CellRangeAddressList(row, row, column, column);
				DataValidationConstraint constraint8 = helper.createFormulaListConstraint(key);
				DataValidation dataValidation3 = helper.createValidation(constraint8, list);
				writeSheetHolder.getSheet().addValidationData(dataValidation3);
			}

		}
	}
}
